<?php
//var_dump($payment_plan_values);
?>

<div class="clearfix"></div>
<div class="alert alert-info"><h4 class="text-info">Payment Plan Table: <span style="text-transform:uppercase;"><?=@$data;?></span></h4></div>
<div>
	<hr class="clearfix">
</div>

<div class="clearfix"></div>

<div id="payment_plan_display" >
<table class="table table-striped table-hover">
	<thead>
		<tr>
			<td>Month</td>
			<td colspan="2" class="text-center text-info">&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td style="text-align:center;">Tuition</td>
			<td style="text-align:center;">Matriculation</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td colspan="3"></td>
		</tr>
	</tbody>
	<?if(@$data_id==1):?>

	<form action="<?=site_url('student_fees/payment_modes');?>" method="POST">
	<tbody>
		<?php foreach($levels as $level){ ?>
		<?php
		$tuit = '';
		$matr = '';
		?>
		<?php 
		if($payment_plan_values!==FALSE){ 
			foreach($payment_plan_values as $ppv){
				if($ppv['payment_mode_id'] == $data_id && $ppv['level_id'] == $level->level_id){
					$tuit = $ppv['payment_plan_tuit'];
					$matr = $ppv['payment_plan_matr'];
				}
			} 
		}
		?>
		<tr>
		<td style="font-weight:bold;"><?php echo $level->level_desc; ?></td>
		<td><input class="table-input annual" name="payment_plan_tuit_<?php echo $level->level_id; ?>" type="text" value="<?php echo $tuit==''?'0.00':$tuit; ?>"></td>
		<td><input class="table-input semestral" name="payment_plan_matr_<?php echo $level->level_id; ?>" type="text" value="<?php echo $matr==''?'0.00':$matr; ?>"></td>
		</tr>
		<?php } ?>
		<tr>
			<td  colspan="3">
				<input type="hidden" name="spl_loc" value="<?=$form_token;?>">
				<input type="hidden" name="type" value="<?=$data;?>">
				<input type="hidden" name="pm_id" value="<?=$data_id;?>">
				<input type="submit" name="set_payment_mode_another" class="btn btn-primary" value="Save Payment Plan">
			</td>
		</tr>
	</tbody>
	</form>

	<?elseif(@$data_id==2):?>
	
	<form action="<?=site_url('student_fees/payment_modes');?>" method="POST">
	<tbody>
		<?php foreach($levels as $level){ ?>
		<?php
		$tuit = '';
		$matr = '';
		?>
		<?php 
		if($payment_plan_values!==FALSE){ 
			foreach($payment_plan_values as $ppv){
				if($ppv['payment_mode_id'] == $data_id && $ppv['level_id'] == $level->level_id){
					$tuit = $ppv['payment_plan_tuit'];
					$matr = $ppv['payment_plan_matr'];
				}
			} 
		}
		?>
		<tr>
		<td style="font-weight:bold;"><?php echo $level->level_desc; ?></td>
		<td><input class="table-input annual" name="payment_plan_tuit_<?php echo $level->level_id; ?>" type="text" value="<?php echo $tuit==''?'0.00':$tuit; ?>"></td>
		<td><input class="table-input semestral" name="payment_plan_matr_<?php echo $level->level_id; ?>" type="text" value="<?php echo $matr==''?'0.00':$matr; ?>"></td>
		</tr>
		<?php } ?>
		<tr>
			<td  colspan="3">
				<input type="hidden" name="spl_loc" value="<?=$form_token;?>">
				<input type="hidden" name="type" value="<?=$data;?>">
				<input type="hidden" name="pm_id" value="<?=$data_id;?>">
				<input type="submit" name="set_payment_mode_another" class="btn btn-primary" value="Save Payment Plan">
			</td>
		</tr>
	</tbody>
	</form>

	<?elseif(@$data_id==3):?>

	<form action="<?=site_url('student_fees/payment_modes');?>" method="POST">
	<tbody>
		<?php foreach($levels as $level){ ?>
		<?php
		$tuit = '';
		$matr = '';
		?>
		<?php 
		if($payment_plan_values!==FALSE){ 
			foreach($payment_plan_values as $ppv){
				if($ppv['payment_mode_id'] == $data_id && $ppv['level_id'] == $level->level_id){
					$tuit = $ppv['payment_plan_tuit'];
					$matr = $ppv['payment_plan_matr'];
				}
			} 
		}
		?>
		<tr>
		<td style="font-weight:bold;"><?php echo $level->level_desc; ?></td>
		<td><input class="table-input annual" name="payment_plan_tuit_<?php echo $level->level_id; ?>" type="text" value="<?php echo $tuit==''?'0.00':$tuit; ?>"></td>
		<td><input class="table-input semestral" name="payment_plan_matr_<?php echo $level->level_id; ?>" type="text" value="<?php echo $matr==''?'0.00':$matr; ?>"></td>
		</tr>
		<?php } ?>
		<tr>
			<td  colspan="3">
				<input type="hidden" name="spl_loc" value="<?=$form_token;?>">
				<input type="hidden" name="type" value="<?=$data;?>">
				<input type="hidden" name="pm_id" value="<?=$data_id;?>">
				<input type="submit" name="set_payment_mode_another" class="btn btn-primary" value="Save Payment Plan">
			</td>
		</tr>
	</tbody>
	</form>

	<?elseif(@$data_id==4):?>

	<form action="<?=site_url('student_fees/payment_modes');?>" method="POST">
	<tbody>
		<?php foreach($levels as $level){ ?>
		<?php
		$tuit = '';
		$matr = '';
		?>
		<?php 
		if($payment_plan_values!==FALSE){ 
			foreach($payment_plan_values as $ppv){
				if($ppv['payment_mode_id'] == $data_id && $ppv['level_id'] == $level->level_id){
					$tuit = $ppv['payment_plan_tuit'];
					$matr = $ppv['payment_plan_matr'];
				}
			} 
		}
		?>
		<tr>
		<td style="font-weight:bold;"><?php echo $level->level_desc; ?></td>
		<td><input class="table-input annual" name="payment_plan_tuit_<?php echo $level->level_id; ?>" type="text" value="<?php echo $tuit==''?'0.00':$tuit; ?>"></td>
		<td><input class="table-input semestral" name="payment_plan_matr_<?php echo $level->level_id; ?>" type="text" value="<?php echo $matr==''?'0.00':$matr; ?>"> </td>
		</tr>
		<?php } ?>
		<tr>
			<td  colspan="3">
				<input type="hidden" name="spl_loc" value="<?=$form_token;?>">
				<input type="hidden" name="type" value="<?=$data;?>">
				<input type="hidden" name="pm_id" value="<?=$data_id;?>">
				<input type="submit" name="set_payment_mode_another" class="btn btn-primary" value="Save Payment Plan">
			</td>
		</tr>
	</tbody>
	</form>

	<?else:?>
		<tr class="info">
			<td  colspan="3">
				Fees is not yet set on this Payment Group.
			</td>
		</tr>
	<?endif;?>
</table>
</div>

<script>

$(document).ready(function() {
	$('#showmethemoney').change(function() {
		var url = $(this).attr('url');
		var opt = $(this).val();
		
		$.get(url, function(data) {
			$('#payment_plan_display').html(data).show(50);
		});
	})
});

//for auto count
(function(){
	var table = $('tbody#<?=$data;?>');
	var annual = table.find('input:text.annual');
	var semestral = table.find('input:text.semestral');
	var trimester = table.find('input:text.trimester');
	var monthly = table.find('input:text.monthly');
	
	
	
	$(table)
	.delegate(annual,'change click hover keyup load',function(){
		var annual_total = 0;
		 $.each( annual, function( key, value ) {
			annual_total += Number(value.value);
		});
		table.find('#total-annual').val(numberWithCommas(annual_total.toFixed(2)));
			
	})
	.delegate(semestral,'change click hover keyup load',function(){	
		var semestral_total = 0;
		 $.each( semestral, function( key, value ) {
			semestral_total += Number(value.value);
		});
		table.find('#total-semestral').val(numberWithCommas(semestral_total.toFixed(2)));
	})
	.delegate(trimester,'change click hover keyup load',function(){	
		var trimester_total = 0;
		 $.each( trimester, function( key, value ) {
			trimester_total += Number(value.value);
		});
		table.find('#total-trimester').val(numberWithCommas(trimester_total.toFixed(2)));
	})
	.delegate(monthly,'change click hover keyup load',function(){	
		var monthly_total = 0;
		 $.each( monthly, function( key, value ) {
			monthly_total += Number(value.value);
		});
		table.find('#total-monthly').val(numberWithCommas(monthly_total.toFixed(2)));
	});
	
	function numberWithCommas(x) 
	{
		var parts = x.toString().split(".");
		parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
		return parts.join(".");
	}
}());
</script>